package com.gdth.bank.model;

import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjJtcybDao;
import com.gdth.database.bean.TAppBfrjZcxxb;
import com.gdth.database.bean.TAppBfrjZcxxbDao;

import org.greenrobot.greendao.query.QueryBuilder;


public class CjkhZcxxModel {
	/**
	 * 查询当前采访客户的所有资产信息
	 * @param
	 * @return
	 */
	public Observable<List<TAppBfrjZcxxb>> getZcxxByUuid(final String jcuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjZcxxb>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjZcxxb>> arg0) {
				// TODO 查询当前采访客户的所有资产信息
				TAppBfrjZcxxbDao dao = BankApplication.getDaoSession().getTAppBfrjZcxxbDao();
				QueryBuilder<TAppBfrjZcxxb> qb = dao.queryBuilder();

				qb.where(TAppBfrjZcxxbDao.Properties.Jcuuid.like(jcuuid),
					qb.or(TAppBfrjZcxxbDao.Properties.Sjzt.eq(AppUtil.sjzt_create), TAppBfrjZcxxbDao.Properties.Sjzt.eq(AppUtil.sjzt_update), TAppBfrjZcxxbDao.Properties.Sjzt.eq(AppUtil.sjzt_normal)));
				qb.orderDesc(TAppBfrjZcxxbDao.Properties.Id);

				List<TAppBfrjZcxxb> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}

	public Observable<List<TAppBfrjJtcyb>> getjtcyxxUuid(final String jcuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjJtcyb>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjJtcyb>> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjJtcybDao dao = BankApplication.getDaoSession().getTAppBfrjJtcybDao();
				QueryBuilder<TAppBfrjJtcyb> qb = dao.queryBuilder();

				qb.where(TAppBfrjJtcybDao.Properties.Jcuuid.eq(jcuuid));
				qb.where(TAppBfrjJtcybDao.Properties.Sjzt.notEq(AppUtil.sjzt_delete));

				List<TAppBfrjJtcyb> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 采访客户的资产信息
	 * @param mTAppBfrjZcxxb
	 * @return
	 */
	public Observable<Long> saveCjkhZcxx(TAppBfrjZcxxb mTAppBfrjZcxxb){
		final TAppBfrjZcxxb tAppBfrjZcxxb = mTAppBfrjZcxxb;
		return Observable.create(new Observable.OnSubscribe<Long>() {

			@Override
			public void call(final Subscriber<? super Long> arg0) {
				// TODO 采访客户的资产信息
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjZcxxbDao dao = session.getTAppBfrjZcxxbDao();
						
						try {
							dao.insertOrReplace(tAppBfrjZcxxb);
							CJkhxxModel.updateCjkhJbxxZt(tAppBfrjZcxxb.getJcuuid());
						} catch(Exception e) {
							ex = e;
							re = false;
						}
						if (re){
							arg0.onNext(tAppBfrjZcxxb.getId());
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}
	
	public Observable<Long> delete(TAppBfrjZcxxb mTAppBfrjZcxxb){
		final TAppBfrjZcxxb tAppBfrjZcxxb = mTAppBfrjZcxxb;
		return Observable.create(new Observable.OnSubscribe<Long>() {

			@Override
			public void call(final Subscriber<? super Long> arg0) {
				// TODO 采访客户的资产信息
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						boolean re = true;
						Exception ex = null;
						TAppBfrjZcxxbDao dao = session.getTAppBfrjZcxxbDao();
						
						try {
							tAppBfrjZcxxb.setSjzt(AppUtil.sjzt_delete);
							dao.insertOrReplace(tAppBfrjZcxxb);
							CJkhxxModel.updateCjkhJbxxZt(tAppBfrjZcxxb.getJcuuid());
						} catch(Exception e) {
							ex = e;
							re = false;
						}
						if (re){
							arg0.onNext(tAppBfrjZcxxb.getId());
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}
}
